<template>
  <div class="detai">
    <detail-nav-bar @clickTitle="clickTitle" ref="navBar"></detail-nav-bar>

    <Scroll class="centent" ref="scroll" @scroll="contentScroll" :probe-type="3">
      <detail-swiper :top-images="topImages"></detail-swiper>
      <!-- 商品信息 -->
      <detail-base-info :goods="Goods"></detail-base-info>
      <!-- 商家信息 -->
      <deta-shop-info :shop="Shop"></deta-shop-info>
      <!-- 详情信息 -->
      <detail-img-info :detail-info="detailInfo" @detailImgLoad="detailImgLoad"></detail-img-info>
      <!-- 商品参数信息 -->
      <detail-params-info :params-info="ParamsInfo" ref="params"></detail-params-info>
      <!-- 用户评价 -->
      <detail-comment-info :comment-info="commentInfo" ref="comment"></detail-comment-info>
      <!-- 详情数据 -->
      <goods-list :goods="recommend" ref="recommend"></goods-list>
    </Scroll>
    <!-- 返回顶部组件 给组件绑定点击事件需要添加修饰符 native-->
    <back-top @click.native="clickTop" v-show="isShowBackTop"></back-top>
    <!-- 底部菜单 -->
    <detail-bottom-btn @addToCart="addToCart"></detail-bottom-btn>
  </div>
</template>

<script>
import DetailNavBar from './childComps/DetailNavBar'
import DetailSwiper from './childComps/DetailSwiper'
import DetailBaseInfo from './childComps/DetailBaseInfo'
import DetaShopInfo from './childComps/DetaShopInfo'
import DetailImgInfo from './childComps/DetailImgInfo'
import DetailParamsInfo from './childComps/DetailParamsInfo'
import DetailCommentInfo from './childComps/DetailCommentInfo'
import Scroll from 'components/common/scroll/Scroll'
import GoodsList from 'components/content/goods/GoodsList'
import DetailBottomBtn from './childComps/DetailBottomBtn'

import { itemListenerMixin, backTopMixin } from 'common/mixin'
import { getDetail, Goods, Shop, getRecommend } from 'network/detail'
import { debounce } from 'common/utils'

export default {
  name: 'Detail',
  data () {
    return {
      // 存储接收过来的商品id
      iid: null,
      // 轮播图数据
      topImages: [],
      // 商品信息
      Goods: {},
      // 商家信息
      Shop: {},
      // 详情信息
      detailInfo: {},
      // 商品详情信息
      ParamsInfo: {},
      // 用户评价信息
      commentInfo: {},
      // 商品详情页数据
      recommend: [],
      // 指定滚动坐标
      themeTopYs: [],
      // 获取组件位置信息
      getThemeTopY: null,
      currentIndex: 0,
      // 商品价格
      price: 0

    }
  },
  created () {
    // 保存传入的id
    this.iid = this.$route.params.iid
    // 根据id 获取数据
    getDetail(this.iid).then(res => {
      const data = res.data.result
      this.topImages = data.itemInfo.topImages
      // 获取商品信息
      this.Goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
      // 创建商家信息
      this.Shop = new Shop(data.shopInfo)
      // 取出详情信息
      this.detailInfo = data.detailInfo
      // 取出商品详情信息
      this.ParamsInfo = data.itemParams
      // 取出用户评价信息
      this.commentInfo = data.rate
      // 获取最低价格
      this.price = data.itemInfo.lowNowPrice

      // 获取位置信息
      // this.$nextTick(() => {
      //   // 使用 nextTick 这个方法， 根据最新的数据对应的dom已经渲染完了 但是图片没有加载完
      //   // 目前获取到的 offsetTop 是不包含图片的
      //   this.themeTopYs = []

      //   this.themeTopYs.push(0)
      //   this.themeTopYs.push(this.$refs.params.$el.offsetTop)
      //   this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
      //   this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
      //   console.log(this.themeTopYs)
      // })
    })
    // 获取详情数据
    getRecommend().then(res => {
      this.recommend = res.data.data.list
    })
    // 给 getThemeTopY 赋值
    this.getThemeTopY = debounce(() => {
      this.themeTopYs = []
      this.themeTopYs.push(0)
      // 对有误差的跳转进行修正
      this.themeTopYs.push(this.$refs.params.$el.offsetTop - 40)
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop - 40)
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop - 40)
      // 数组中传入 js 最大值
      this.themeTopYs.push(Number.MAX_VALUE)
    })
  },
  // 混入配置
  mixins: [itemListenerMixin, backTopMixin],
  // 离开页面 取消事件绑定(详情页面没有做缓存 需要在 destroyed 取消事件)
  methods: {
    detailImgLoad () {
      //  详情页图片加载完成重新计算高度
      // this.$refs.scroll.refresh()
      this.newRefresh()
      // 在这里获取各个组件的位置信息是正确的
      this.getThemeTopY()
    },
    clickTitle (index) {
      // 滚动到指定位置
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 1000)
    },
    // 监听滚动事件
    // positionY 和 内容区对比
    // [0, 9746, 10488, 10683, Number.MAX_VALUE]
    contentScroll (position) {
      let positionY = -position.y
      let length = this.themeTopYs.length
      for (let i = 0; i < length - 1; i++) {
        if (this.currentIndex !== i && (positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1])) {
          this.currentIndex = i
          this.$refs.navBar.currentIndex = this.currentIndex
        }
      }
      // 判断 屏幕滚动 y值大于1000 显示返回顶部组件 (因为y值是负值 所以要转正再比较)
      this.isShowBackTop = (-position.y) > 1000
      // 决定tabControl是否吸顶 (position:fixed)
      this.isFixed = (-position.y) > this.tabOffsetTop
    },
    addToCart () {
      const product = {}
      product.img = this.topImages[0]
      product.iid = this.iid
      product.title = this.Goods.title
      product.desc = this.Goods.desc
      product.price = this.price

      // 将商品添加到购物车
      this.$store.dispatch('addCart', product)
    }
  },
  destroyed () {
    this.$bus.$off('imageLoad', this.itemImgLoad)
  },

  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetaShopInfo,
    DetailImgInfo,
    DetailParamsInfo,
    Scroll,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBtn
  }
}
</script>

<style scoped>
.detai {
  height: 100vh;
  position: relative;
  background-color: #fff;
  z-index: 10;
  overflow: hidden;
}
.centent {
  height: calc(100% - 44px);
}
</style>
